package datastructure.ch04_stacks.ch03_array_stack;

import datastructure.ch02_arrays.ch06_generic_data.ArrayList;
import datastructure.ch04_stacks.ch02_interface_abstarct.Stack;

/**
 * 栈（stack）
 *
 * @author guodd
 * @version 1.0
 * 顺序栈的实现
 */
public class ArrayStack<E> implements Stack<E> {
    public static void main(String[] args) {
        ArrayStack<Integer> arrayStack = new ArrayStack<>(10);
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.getSize());
        System.out.println(arrayStack.peek());
    }

    // 自己封装的动态数组
    ArrayList<E> array;

    public ArrayStack() {
        array = new ArrayList<>(10);
    }

    public ArrayStack(int capacity) {
        array = new ArrayList<>(capacity);
    }

    @Override
    public void push(E e) {
        array.addLast(e);
    }

    @Override
    public E pop() {
        return array.removeLast();
    }

    @Override
    public E peek() {
        return array.getLast();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void clear() {
        array.clear();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    public int getCapacity() {
        return array.getCapacity();
    }
}
